import compose from "koa-compose";

export interface Template {
    init: { x: number; y: number };
    speed: number;
    angle: number;
    angleSpeed?: number;
}

export abstract class Bullet {
    constructor() {}
    tracks: compose.Middleware<Template>[] = [];

    destroy() {}

    /**
     * @default 数值弧度
     */
    private angle: number = 0;
    speed = 20;
    angleSpeed = 0;

    /**
     * 设置角度，支持 Angle，使用 角度 "30"（string）、Math.PI/4（数值）
     * @param angle
     * @returns this
     */
    setAngle(angle: { x: number; y: number } | string | number) {
        switch (typeof angle) {
            case "string":
                angle = Laya.Utils.toRadian(parseFloat(angle));
            case "number":
                this.angle = angle;
                break;
            default:
                this.angle = Math.atan(angle.y / angle.x);
                if (angle.x < 0) this.angle += Math.PI;
        }
        return this;
    }
    changeAngle(func: (number: number) => number) {
        this.angle = Laya.Utils.toRadian(func(Laya.Utils.toAngle(this.angle)));
        return this;
    }
    setSpeed(speed: number) {
        this.speed = speed;
        return this;
    }
    setAngleSpeed(speed: number) {
        this.angleSpeed = speed;
        return this;
    }

    /**
     * 射击方式，在射击前需要使用 setSpeed 设置速度哦
     * @param from 提供 x,y 起始坐标即可
     */

    async shoot(from: { x: number; y: number }) {
        const data = {
            init: from,
            speed: this.speed,
            angle: this.angle,
            angleSpeed: 0,
        };
        const composed = compose<Template>(this.tracks);
        await composed(data);
        return this.render(data);
    }

    /** 渲染函数，用于将子弹渲染到舞台上，需要被子类实现 */
    abstract render(Template: Template): Laya.Sprite;
}

export default Bullet;
